/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.NVBH;

import DAO.CTHoaDonDao;
import DAO.HoaDonDao;
import DAO.KhachHangDao;
import DAO.LoaiSanPhamDao;
import DAO.SanPhamDao;
import GUI.Var;
import GUI.TOOL.InHoaDon;
import POJO.CTHoaDon;
import POJO.HoaDon;
import POJO.KhachHang;
import POJO.LoaiSanPham;
import POJO.SanPham;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Khoi
 */
public class frLapHoaDon extends javax.swing.JInternalFrame {

	/**
	 * Creates new form frLapHoaDon
	 */
	private DefaultComboBoxModel cbModel;
	private DefaultComboBoxModel cbModel2;
	private DefaultComboBoxModel cbModelDanhSachKhachHang;
	private DefaultTableModel tbModel;
	private boolean load;
	private ArrayList<CTHoaDon> dsCT = null;

	public frLapHoaDon() {
		initComponents();
		txMaNV.setText(Integer.toString(Var.NV.getMaNV()));
		txTenNV.setText(Var.NV.getTenNV());

		load = false;

		//load thong tin ve Ma ngay
		int ng, t, n;
		ng = Var.NGAY.get(Calendar.DATE);
		t = Var.NGAY.get(Calendar.MONTH) + 1;
		n = Var.NGAY.get(Calendar.YEAR);

		String stn;
		stn = ng + "-" + t + "-" + n;
		txNgayLap.setText(stn);
		// khoi tao cac cobox
		this.cbModel = new DefaultComboBoxModel();
		this.cbModel2 = new DefaultComboBoxModel();
		this.cbModelDanhSachKhachHang = new DefaultComboBoxModel();
		// gan ca model da khoi tao vao combo
		this.cbLoaiSP.setModel(cbModel);
		this.cbSanPham.setModel(cbModel2);
		this.cbDanhSachKhachHang = new javax.swing.JComboBox();

		this.cbDanhSachKhachHang.setModel(cbModelDanhSachKhachHang);
		// hien thi cbDS khach hang
		cbDanhSachKhachHang.setLocation(txTenKH.getLocation());
		cbDanhSachKhachHang.setSize(txTenKH.getSize());
		cbDanhSachKhachHang.setVisible(cbModelDanhSachKhachHang.getSize() > 1);
		this.jPanel2.add(cbDanhSachKhachHang);


		//  tao moi Danh sach chi tiet hoa don	
		DefaultFrom();
		loadCobo();

		// chon loai san pham dau tien
		cbModel.setSelectedItem(cbModel.getElementAt(0));

		LoaiSanPham tmp = (LoaiSanPham) cbModel.getSelectedItem();
		// load danh sach san pham
		loadCombo2(tmp);

		// dang ki su kien ch cb Danh sach khach hang
		cbDanhSachKhachHang.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cbDanhSachKhachHang(evt);
			}
		});
	}

	private void cbDanhSachKhachHang(java.awt.event.ActionEvent evt) {
		if (cbModelDanhSachKhachHang.getSize() > 0) {
			if (cbModelDanhSachKhachHang.getSelectedItem() == null) {
				cbModelDanhSachKhachHang.setSelectedItem(cbModelDanhSachKhachHang.getElementAt(0));
			}
			KhachHang ob = (KhachHang) cbModelDanhSachKhachHang.getSelectedItem();
			txMaKH.setText(Integer.toString(ob.getMaKH()));
			txSDT.setText(ob.getDienThoai());
		}
	}

	private void loadCobo() {
		load = false;
		LoaiSanPhamDao dao = new LoaiSanPhamDao();

		ArrayList<LoaiSanPham> ds = new ArrayList<LoaiSanPham>();
		ds = dao.layDanhSach();
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel.addElement(ds.get(i));
		}
		if (cbModel.getSize() > 0) {
			cbModel.setSelectedItem(cbModel.getElementAt(0));//chon loai san pham dau tien
		}
		load = true;
	}

	private void loadCombo2(LoaiSanPham lsp) {
		SanPhamDao dao = new SanPhamDao();

		ArrayList<SanPham> ds = new ArrayList<SanPham>();

		ds = dao.layDanhSachByMaLSPDeBan(lsp.getMaLSP());
		cbModel2.removeAllElements();// lam rong cb truoc khi them
		for (int i = 0; i < ds.size(); i++) {
			this.cbModel2.addElement(ds.get(i));
		}
		if (cbModel2.getSize() > 0) {
			cbModel2.setSelectedItem(cbModel2.getElementAt(0));// chon san pham dau tien
		}

	}

	private int tonTaiTaiViTri(int MaSP) {
		for (int i = 0; i < tbModel.getRowCount(); i++) {
			int tmp = ((Integer) tbModel.getValueAt(i, 1));
			if (tmp == MaSP) {
				return i;
			}
		}
		return -1;
	}

	public void DefaultFrom() {
		tbModel = new DefaultTableModel();
		// tao cac cot
		this.tbModel.addColumn("STT");
		this.tbModel.addColumn("Mã sản phẩm");
		this.tbModel.addColumn("Tên sản phẩm");
		this.tbModel.addColumn("Số lượng ");
		this.tbModel.addColumn("Giá ");
		this.tbModel.addColumn("Tổng tiền ");

		dsCT = new ArrayList<CTHoaDon>();


	}

	private int laySoLuong() {
		try {
			int sl = Integer.parseInt(txSoLuong.getText().trim());
			sl = Math.abs(sl);
			txSoLuong.setText(Integer.toString(sl));
			return sl;
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhập sai số lượng. Nhập lại:", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			txSoLuong.requestFocus();
			return 0;
		}

	}

	private double layGiaBan() {
		
			double gi = Double.parseDouble(lbGia.getText().trim());
			gi = Math.abs(gi);
			lbGia.setText(Double.toString(gi));
			
			return gi;
		
	}

	private void themSanPhamVaoTbModel() {
		if (cbSanPham.getSelectedItem() != null) {
			CTHoaDon tmp = new CTHoaDon();
			SanPham sp = new SanPham((SanPham) cbSanPham.getSelectedItem());
			tmp.setMaSP(sp.getMaSP());
			int vt = tonTaiTaiViTri(sp.getMaSP());
			if (vt == -1) {
				int sl = laySoLuong();
				if (sl == 0) {
					return;
				}
				if (sl >sp.getSlTon())//khong du so luong de xuat
				{
					JOptionPane.showMessageDialog(this, "Không đủ số lượng xuất.","Thông báo lỗi ",JOptionPane.ERROR_MESSAGE);
					txSoLuong.requestFocus();;
					return;
				}
				tmp.setSoLuong(sl);
				
				double gi = layGiaBan();
				tmp.setGiaBan(gi);
				tmp.setMaHD(0);
				//	dsCT.add(tmp);
				Object[] items = new Object[]{
					0,
					tmp.getMaSP(),
					sp.getTenSP(),
					tmp.getSoLuong(),
					tmp.getGiaBan(),
					0.0,
					tmp.getMaCTHD()
				};

				tbModel.addRow(items);
				//}
			} else {
				Integer ob = (Integer) tbModel.getValueAt(vt, 3);
				ob += laySoLuong();
				if (ob >sp.getSlTon())//khong du so luong de xuat
				{
					JOptionPane.showMessageDialog(this, "Không đủ số lượng xuất.","Thông báo lỗi ",JOptionPane.ERROR_MESSAGE);
					txSoLuong.requestFocus();;
					return;
				}
				else // du so luong thi cong them vao
				{
					Object o = ob;
					tbModel.setValueAt(o, vt, 3);
				}
			}
			for (int i = 0; i < tbModel.getRowCount(); i++)// cap nhat STT // cap nhat tong tien
			{
				Object o = i + 1;// stt
				tbModel.setValueAt(o, i, 0);

				// cap nha tong tien
				Object os = tbModel.getValueAt(i, 3);
				Object og = tbModel.getValueAt(i, 4);

				double tt = (Integer) os * (Double) og;

				Object ot = tt;
				tbModel.setValueAt(ot, i, 5);


			}
		}
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              jPanel2 = new javax.swing.JPanel();
              txMaKH = new javax.swing.JTextField();
              jLabel6 = new javax.swing.JLabel();
              jLabel5 = new javax.swing.JLabel();
              txTenKH = new javax.swing.JTextField();
              jLabel7 = new javax.swing.JLabel();
              txSDT = new javax.swing.JTextField();
              jButton1 = new javax.swing.JButton();
              btKhachMoi = new javax.swing.JButton();
              jCheckBox1 = new javax.swing.JCheckBox();
              jLabel18 = new javax.swing.JLabel();
              txTenNV = new javax.swing.JTextField();
              jLabel4 = new javax.swing.JLabel();
              txMaNV = new javax.swing.JTextField();
              jButton2 = new javax.swing.JButton();
              jPanel3 = new javax.swing.JPanel();
              jPanel4 = new javax.swing.JPanel();
              cbLoaiSP = new javax.swing.JComboBox();
              cbSanPham = new javax.swing.JComboBox();
              jLabel8 = new javax.swing.JLabel();
              jLabel9 = new javax.swing.JLabel();
              txSoLuong = new javax.swing.JTextField();
              jLabel10 = new javax.swing.JLabel();
              btThem = new javax.swing.JButton();
              btXoa = new javax.swing.JButton();
              jLabel14 = new javax.swing.JLabel();
              jLabel19 = new javax.swing.JLabel();
              lbCoTheBan = new javax.swing.JLabel();
              lbGia = new javax.swing.JLabel();
              jPanel6 = new javax.swing.JPanel();
              jScrollPane1 = new javax.swing.JScrollPane();
              tbChiTiet = new javax.swing.JTable();
              jPanel5 = new javax.swing.JPanel();
              txGiam = new javax.swing.JTextField();
              txTriGia = new javax.swing.JTextField();
              jLabel12 = new javax.swing.JLabel();
              btLapHoaDon = new javax.swing.JButton();
              btInHoaDon = new javax.swing.JButton();
              jLabel15 = new javax.swing.JLabel();
              jLabel16 = new javax.swing.JLabel();
              jLabel17 = new javax.swing.JLabel();
              jLabel13 = new javax.swing.JLabel();
              cbTT = new javax.swing.JComboBox();
              jPanel7 = new javax.swing.JPanel();
              jLabel2 = new javax.swing.JLabel();
              jLabel3 = new javax.swing.JLabel();
              txMaHoaDon = new javax.swing.JTextField();
              txNgayLap = new javax.swing.JTextField();

              setClosable(true);
              setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
              setIconifiable(true);
              setMaximizable(true);
              setResizable(true);

              jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel1.setText("LẬP HÓA ĐƠN");

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1)
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng / người lập hóa đơn"));
              jPanel2.setAutoscrolls(true);
              jPanel2.addComponentListener(new java.awt.event.ComponentAdapter() {
                     public void componentResized(java.awt.event.ComponentEvent evt) {
                            jPanel2ComponentResized(evt);
                     }
                     public void componentShown(java.awt.event.ComponentEvent evt) {
                            jPanel2ComponentShown(evt);
                     }
              });
              jPanel2.addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener() {
                     public void ancestorMoved(java.awt.event.HierarchyEvent evt) {
                     }
                     public void ancestorResized(java.awt.event.HierarchyEvent evt) {
                            jPanel2AncestorResized(evt);
                     }
              });
              jPanel2.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
                     public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                            jPanel2VetoableChange(evt);
                     }
              });

              txMaKH.setEnabled(false);

              jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jLabel6.setText("Tên khách hàng:");

              jLabel5.setText("Mã khách hàng:");

              jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jLabel7.setText("Số điện thoại:");

              txSDT.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            txSDTKeyReleased(evt);
                     }
              });

              jButton1.setText("Tìm");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                     }
              });

              btKhachMoi.setText("Khách hàng mới");
              btKhachMoi.setToolTipText("Nhập thêm khách hàng mới");
              btKhachMoi.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btKhachMoiActionPerformed(evt);
                     }
              });

              jCheckBox1.setText("Mở");
              jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jCheckBox1ActionPerformed(evt);
                     }
              });

              jLabel18.setText("Nhân viên bán:");

              txTenNV.setEnabled(false);
              txTenNV.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txTenNVActionPerformed(evt);
                     }
              });

              jLabel4.setText("Mã nhân viên:");

              txMaNV.setEnabled(false);
              txMaNV.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txMaNVActionPerformed(evt);
                     }
              });

              jButton2.setText("Hóa đơn mới");
              jButton2.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton2ActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                   .addComponent(txTenKH, javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(txMaKH, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                   .addComponent(txSDT))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel2Layout.createSequentialGroup()
                                          .addComponent(btKhachMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(jButton2)
                                          .addContainerGap())
                                   .addGroup(jPanel2Layout.createSequentialGroup()
                                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jCheckBox1, javax.swing.GroupLayout.Alignment.LEADING)
                                                 .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                 .addComponent(jLabel4)
                                                 .addComponent(jLabel18))
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                 .addComponent(txMaNV, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                                                 .addComponent(txTenNV)))))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel5)
                                   .addComponent(txMaKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jCheckBox1)
                                   .addComponent(txMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel6)
                                   .addComponent(txTenKH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jButton1)
                                   .addComponent(jLabel18)
                                   .addComponent(txTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(txSDT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel7)
                                   .addComponent(btKhachMoi)
                                   .addComponent(jButton2)))
              );

              jPanel4.setToolTipText("");

              cbLoaiSP.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
              cbLoaiSP.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cbLoaiSPActionPerformed(evt);
                     }
              });

              cbSanPham.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
              cbSanPham.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cbSanPhamActionPerformed(evt);
                     }
              });

              jLabel8.setText("Loại sản phẩm:");

              jLabel9.setText("Sản phẩm:");

              txSoLuong.setText("1");

              jLabel10.setText("Số lượng:");

              btThem.setText("+");
              btThem.setToolTipText("Thêm sản phẩm xuống danh sách bên dưới để lập hóa đơn bán");
              btThem.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btThemActionPerformed(evt);
                     }
              });

              btXoa.setText("-");
              btXoa.setToolTipText("Chọn sản phẩm bên dưới sau đó xóa khỏi danh sách hóa đơn.");
              btXoa.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btXoaActionPerformed(evt);
                     }
              });

              jLabel14.setText("VNĐ");

              jLabel19.setText("Có thể bán:");

              lbCoTheBan.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbCoTheBan.setText("0");

              lbGia.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              lbGia.setText("0");

              javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
              jPanel4.setLayout(jPanel4Layout);
              jPanel4Layout.setHorizontalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGap(36, 36, 36)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel9)
                                   .addComponent(jLabel8)
                                   .addComponent(jLabel19))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                   .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                                          .addComponent(lbCoTheBan, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(jLabel10)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(txSoLuong, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                                   .addComponent(cbLoaiSP, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(cbSanPham, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                                   .addGroup(jPanel4Layout.createSequentialGroup()
                                          .addComponent(lbGia)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(jLabel14)))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel4Layout.setVerticalGroup(
                     jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(cbLoaiSP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel8))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(cbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel9)
                                   .addComponent(lbGia)
                                   .addComponent(jLabel14))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(lbCoTheBan)
                                   .addComponent(jLabel10)
                                   .addComponent(txSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel19)
                                   .addComponent(btXoa)
                                   .addComponent(btThem))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết hóa đơn"));

              tbChiTiet.setModel(new javax.swing.table.DefaultTableModel(
                     new Object [][] {

                     },
                     new String [] {
                            "STT", "Mã SP", "Tên Sản phẩm", "Số lượng", "Giá", "Tổng tiền"
                     }
              ));
              jScrollPane1.setViewportView(tbChiTiet);

              javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
              jPanel6.setLayout(jPanel6Layout);
              jPanel6Layout.setHorizontalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
              );
              jPanel6Layout.setVerticalGroup(
                     jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
              );

              javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
              jPanel3.setLayout(jPanel3Layout);
              jPanel3Layout.setHorizontalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              );
              jPanel3Layout.setVerticalGroup(
                     jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              txGiam.setText("0.0");
              txGiam.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            txGiamActionPerformed(evt);
                     }
              });
              txGiam.addKeyListener(new java.awt.event.KeyAdapter() {
                     public void keyReleased(java.awt.event.KeyEvent evt) {
                            txGiamKeyReleased(evt);
                     }
              });

              txTriGia.setEditable(false);
              txTriGia.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

              jLabel12.setText("Số tiền giảm:");

              btLapHoaDon.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              btLapHoaDon.setText("Lập hóa đơn");
              btLapHoaDon.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btLapHoaDonActionPerformed(evt);
                     }
              });

              btInHoaDon.setText("In hóa đơn");
              btInHoaDon.setToolTipText("In hóa đơn");
              btInHoaDon.setEnabled(false);
              btInHoaDon.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btInHoaDonActionPerformed(evt);
                     }
              });

              jLabel15.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jLabel15.setText("Phải trả:");

              jLabel16.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              jLabel16.setText("VNĐ");

              jLabel17.setText("VNĐ");

              jLabel13.setText("Trạng thái:");

              cbTT.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Đã thanh toán", "Chưa thanh toán" }));
              cbTT.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cbTTActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
              jPanel5.setLayout(jPanel5Layout);
              jPanel5Layout.setHorizontalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(txGiam, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                   .addComponent(txTriGia))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addComponent(jLabel17)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                          .addComponent(jLabel13))
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addComponent(jLabel16)
                                          .addGap(0, 0, Short.MAX_VALUE)))
                            .addGap(14, 14, 14)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(cbTT, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addGroup(jPanel5Layout.createSequentialGroup()
                                          .addComponent(btInHoaDon)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                          .addComponent(btLapHoaDon)))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );
              jPanel5Layout.setVerticalGroup(
                     jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel5Layout.createSequentialGroup()
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(jLabel12)
                                          .addComponent(txGiam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addComponent(jLabel17))
                                   .addComponent(jLabel13)
                                   .addComponent(cbTT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(jLabel15)
                                          .addComponent(txTriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addComponent(jLabel16))
                                   .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                          .addComponent(btInHoaDon)
                                          .addComponent(btLapHoaDon)))
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              jLabel2.setText("Mã hóa đơn:");

              jLabel3.setText("Ngày lập:");

              txMaHoaDon.setEnabled(false);

              txNgayLap.setEnabled(false);

              javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
              jPanel7.setLayout(jPanel7Layout);
              jPanel7Layout.setHorizontalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel7Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(jPanel7Layout.createSequentialGroup()
                                          .addComponent(jLabel3)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(txNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                                   .addGroup(jPanel7Layout.createSequentialGroup()
                                          .addComponent(jLabel2)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                          .addComponent(txMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap())
              );
              jPanel7Layout.setVerticalGroup(
                     jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel7Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel2)
                                   .addComponent(txMaHoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel3)
                                   .addComponent(txNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 0, 0))
              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addGroup(layout.createSequentialGroup()
                                          .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                          .addGap(0, 0, 0)
                                          .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                   .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                   .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
              );
              layout.setVerticalGroup(
                     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                   .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 0, 0)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(1, 1, 1)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0))
              );

              pack();
       }// </editor-fold>//GEN-END:initComponents

	private double layTienGiam() {
		try {
			return Double.parseDouble(txGiam.getText().trim());

		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhập sai tiền giảm. Nhập lại:", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			txGiam.selectAll();
			txGiam.requestFocus();
			return 0;
		}

	}

	private void tinhTongTien() {
		double tmp = 0.0;
		for (int i = 0; i < tbModel.getRowCount(); i++) {
			Object ob = tbModel.getValueAt(i, 5);
			tmp = tmp + (Double) ob;
		}
		double t = layTienGiam();
		tmp = tmp - t;
		tmp = (tmp < 0) ? 0 : tmp;
		txTriGia.setText(Double.toString(tmp));

	}
       private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
		// TODO add your handling code here:
		btInHoaDon.setEnabled(false);
		themSanPhamVaoTbModel();
		tbChiTiet.setModel(tbModel);// set modle vao JTable
		// Canh deu cac dong theo ti le % 
		// co tong cong 6 cot
		int[] va = {6, 13, 30, 7, 20, 23};
		int[] pt = new int[6];
		for (int l = 0; l < 6; l++) {
			pt[l] = tbChiTiet.getWidth() * va[l] / 100;
			tbChiTiet.getColumnModel().getColumn(l).setPreferredWidth(pt[l]);

		}
		tinhTongTien();

       }//GEN-LAST:event_btThemActionPerformed
	private void daThemXongKhachHang() {
		btKhachMoi.setText("Khách hàng mới");
		txMaKH.setEnabled(true);

	}

	private boolean kiemTraKhongNhapKhachHang() {
		if (txTenKH.getText().equals("")) {
			return true;
		}
		if (txSDT.getText().equals("")) {
			return true;
		}
		return false;

	}
       private void cbLoaiSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLoaiSPActionPerformed
		// TODO add your handling code here:
		if (load) {
			LoaiSanPham tmp = (LoaiSanPham) cbModel.getSelectedItem();
			// load danh sach san pham
			loadCombo2(tmp);
		}
       }//GEN-LAST:event_cbLoaiSPActionPerformed

       private void cbSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbSanPhamActionPerformed
		// TODO add your handling code here:
		if (load) {
			if (cbModel2.getSize() > 0) {
				SanPham sp = (SanPham) cbSanPham.getSelectedItem();
				lbCoTheBan.setText(Integer.toString(sp.getSlTon()));
				lbGia.setText(Double.toString(sp.getGiaBanHienHanh()));
			}
		}
       }//GEN-LAST:event_cbSanPhamActionPerformed
	private void themTextKhachHang(KhachHang tmp) {
		// ham nay can viet lai
		txMaKH.setText(Integer.toString(tmp.getMaKH()));
		txTenKH.setText(tmp.getTenKH());
		txSDT.setText(tmp.getDienThoai());
	}

	private void xoaDSKH() {
		cbModelDanhSachKhachHang.removeAllElements();
	}

	private void botCBKhachHang() {
		cbDanhSachKhachHang.setVisible(false);
		txTenKH.setVisible(true);
	}

	private void botTextKhachHang() {
		txTenKH.setVisible(false);
		cbDanhSachKhachHang.setVisible(true);

	}

	private void themCBKhachHang(ArrayList<KhachHang> dsk) {
		if (dsk != null) {
			xoaDSKH();
			for (int i = 0; i < dsk.size(); i++) {
				cbModelDanhSachKhachHang.addElement(dsk.get(i));
			}
			cbDanhSachKhachHang.setModel(cbModelDanhSachKhachHang);
		}
		cbDanhSachKhachHang.setLocation(txTenKH.getLocation());
		cbDanhSachKhachHang.setSize(txTenKH.getSize());
		cbDanhSachKhachHang.setVisible(true);
		botTextKhachHang();

	}

	private void xoaTexBoxKH() {
		txMaKH.setText("");
		txTenKH.setText("");
		txSDT.setText("");
	}

	private void xoaKhoiTbModel() {
		int dong = tbChiTiet.getSelectedRow();
		if (dong >= 0 && dong < tbModel.getRowCount()) {
			tbModel.removeRow(dong);
		} else {
			JOptionPane.showMessageDialog(this, "Chọn dòng cần xóa", "Cảnh báo!!!", JOptionPane.WARNING_MESSAGE);

		}
	}
       private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
		// TODO add your handling code here:
		xoaKhoiTbModel();
		tinhTongTien();
       }//GEN-LAST:event_btXoaActionPerformed

       private void txGiamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txGiamActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txGiamActionPerformed

       private void txGiamKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txGiamKeyReleased
		// TODO add your handling code here:
		tinhTongTien();
       }//GEN-LAST:event_txGiamKeyReleased

	private void xoaDSCT() {
		// xoa danh sach chi tiet
		int i = dsCT.size() - 1;
		while (i >= 0) {
			dsCT.remove(i);
			i--;
		}

	}

	private boolean kiemTraCoKhachHang() {

		if (txMaKH.getText().equals("")) {
			return false;
		}
		return true;

	}

	private void lapHoaDonBanHang() {
		if (tbModel.getRowCount() <= 0) {
			JOptionPane.showMessageDialog(this, "Chọn sản phẩm cần bán sau đó thêm vào danh sách.", "Thông báo.", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		if (kiemTraCoKhachHang() == false) {
			JOptionPane.showMessageDialog(this, "Nhập khách hàng.", "Thông báo.", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		xoaDSCT();
		int[] d = new int[3];
		String st = txNgayLap.getText();
		String[] s = st.split("-");
		d[0] = Integer.parseInt(s[0]);
		d[1] = Integer.parseInt(s[1]);
		d[2] = Integer.parseInt(s[2]);

		Date dt = new Date(d[2], d[1] - 1, d[0]);// co 12 thang , thang dau tien bat dau = 0
		int nv = Var.NV.getMaNV();
		double gi = Double.parseDouble(txGiam.getText());
		double tg = Double.parseDouble(txTriGia.getText()) + gi;

		int trangThai = 1;// chưa thanh toán chưa giao hàng

		if (cbTT.getSelectedIndex() == 0) {
			trangThai = 2;//Thanh toán chưa giao hàng
		}

		int kh = Integer.parseInt(txMaKH.getText());
		HoaDon hd = new HoaDon(0, dt, tg, gi, trangThai, nv, kh);

		HoaDonDao dao = new HoaDonDao();
		try {
			dao.them(hd);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Lỗi không thể lập hóa đơn (1):");
			return;
		}
		txMaHoaDon.setText(Integer.toString(hd.getMaHD()));
		for (int i = 0; i < tbModel.getRowCount(); i++) {
			int ma = (Integer) tbModel.getValueAt(i, 1);
			int sl = (Integer) tbModel.getValueAt(i, 3);
			double gia = (Double) tbModel.getValueAt(i, 4);

			CTHoaDon ct = new CTHoaDon(0, sl, gia, hd.getMaHD(), ma);
			dsCT.add(ct);
		}
		// them chi tiet
		CTHoaDonDao daoct = new CTHoaDonDao();
		try {
			if (daoct.them(dsCT)) {
				JOptionPane.showMessageDialog(this, "Lập hóa đơn thành công. ", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
				btInHoaDon.setEnabled(true);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Lỗi không thể thêm chi tiết (2):");
			return;

		}
	}
       private void btLapHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLapHoaDonActionPerformed
		// TODO add your handling code here:
		lapHoaDonBanHang();
       }//GEN-LAST:event_btLapHoaDonActionPerformed

       private void btInHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInHoaDonActionPerformed
		// TODO add your handling code here:
		DefaultTableModel mdhd = new DefaultTableModel(new String[]{"TieuDe", "NoiDung"}, 0);
		mdhd.addRow(new String[]{"Mã hóa đơn: ", txMaHoaDon.getText()});
		mdhd.addRow(new String[]{"Ngày lập: ", txNgayLap.getText()});
		mdhd.addRow(new String[]{"Mã khách hàng:", txMaKH.getText()});
		mdhd.addRow(new String[]{"Tên khách hàng", txTenKH.getText()});
		mdhd.addRow(new String[]{"Số điện thoại:", txSDT.getText()});
		mdhd.addRow(new String[]{"Mã nhân viên", txMaNV.getText()});
		mdhd.addRow(new String[]{"Nhân viên:", txTenNV.getText()});

		mdhd.addRow(new String[]{"", ""});

		Object ob = cbTT.getSelectedItem();
		mdhd.addRow(new String[]{"Trạng thái hóa đơn:", ob.toString()});

		Double tienGiam = Double.parseDouble(txGiam.getText());
		Double triGia = Double.parseDouble(txTriGia.getText()) + tienGiam;

		mdhd.addRow(new String[]{"Tổng tiền", triGia.toString()});
		mdhd.addRow(new String[]{"", "-"});
		mdhd.addRow(new String[]{"Số tiền giản", tienGiam.toString()});
		mdhd.addRow(new String[]{"", ""});
		mdhd.addRow(new String[]{"Phải trả", txTriGia.getText()});

		DefaultTableModel cuoiTrang = new DefaultTableModel(new String[]{"Khoang cach","TieuDe", "NoiDung"}, 0);
		cuoiTrang.addRow(new String[]{"","Người bán ký tên", "Khách hàng kí tên"});


		InHoaDon.MainClass(mdhd, tbModel, cuoiTrang);


       }//GEN-LAST:event_btInHoaDonActionPerformed

       private void cbTTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTTActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_cbTTActionPerformed

	private void xoaChiTietHoaDon() {
		//xoa tbModel
		int i = tbModel.getRowCount() - 1;
		while (i >= 0) {
			tbModel.removeRow(i);
			i--;
		}
		txTriGia.setText("");
		xoaDSCT();

	}
       private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
		// TODO add your handling code here:
		Object ob = cbSanPham.getSelectedItem();
		SanPham sp = (SanPham)ob;
		int tmp =SanPhamDao.laySoLuongCoTheXuat(sp.getMaSP());
		lbCoTheBan.setText(String.valueOf(tmp));
		xoaTexBoxKH();
		xoaChiTietHoaDon();
		txMaHoaDon.setText("");

       }//GEN-LAST:event_jButton2ActionPerformed

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		daThemXongKhachHang();
		int ma = 0;
		try {
			ma = txMaKH.getText().length() > 0 ? Integer.parseInt(txMaKH.getText()) : 0;
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Sai mã khách hàng .", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
		}
		String ten = txTenKH.getText().trim();
		String sdt = txSDT.getText().trim();
		KhachHang kh = new KhachHang(ma, ten, "", sdt);
		KhachHangDao dao = new KhachHangDao();
		ArrayList<KhachHang> dskh = new ArrayList<KhachHang>();

		try {
			dskh = dao.timKiem(kh);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Không tìm thấy");
		}
		if (dskh.size() == 1) {
			// tim thay 1
			themTextKhachHang(dskh.get(0));
			botCBKhachHang();
		} else if (dskh.size() > 1) {
			// tim thay nhieu
			// an texbox ten khach hang
			// hien cbBo danh sach khach hang
			botTextKhachHang();
			themCBKhachHang(dskh);

		} else {
			// khong tim thay
		}
       }//GEN-LAST:event_jButton1ActionPerformed

       private void btKhachMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKhachMoiActionPerformed
		// TODO add your handling code here:
		if (btKhachMoi.getText().equals("Khách hàng mới") == true) {
			btKhachMoi.setText("Thêm khách");
			txMaKH.setEnabled(false);
			botCBKhachHang();
			txMaKH.setText("");
			txSDT.setText("");
			txTenKH.setFocusable(true);

		} else {
			if (kiemTraKhongNhapKhachHang()) {
				JOptionPane.showMessageDialog(this, "Nhập đủ thông tin khách hàng để thêm. Tên , số điện thoại");
				txTenKH.requestFocus();
				return;
			}
			KhachHang khn = new KhachHang();
			khn.setTenKH(txTenKH.getText());
			khn.setDienThoai(txSDT.getText());
			KhachHangDao dao = new KhachHangDao();
			try {
				if (dao.them(khn)) {
					txMaKH.setText(Integer.toString(khn.getMaKH()));
					JOptionPane.showMessageDialog(this, "Đã thêm khách hàng");
				}

			} catch (Exception e) {
			}
			daThemXongKhachHang();
		}
       }//GEN-LAST:event_btKhachMoiActionPerformed

       private void txTenNVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txTenNVActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txTenNVActionPerformed

       private void txMaNVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txMaNVActionPerformed
		// TODO add your handling code here:
       }//GEN-LAST:event_txMaNVActionPerformed

       private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
		// TODO add your handling code here:
		daThemXongKhachHang();
		if (jCheckBox1.isSelected() == true) {
			txMaKH.setEnabled(true);
			xoaTexBoxKH();
			botCBKhachHang();
		} else {
			txMaKH.setEnabled(false);
			xoaTexBoxKH();
			botTextKhachHang();
		}
       }//GEN-LAST:event_jCheckBox1ActionPerformed

       private void jPanel2VetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_jPanel2VetoableChange
		// TODO add your handling code here:
       }//GEN-LAST:event_jPanel2VetoableChange

       private void jPanel2ComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel2ComponentShown
		// TODO add your handling code here:
       }//GEN-LAST:event_jPanel2ComponentShown

       private void jPanel2ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel2ComponentResized
		// TODO add your handling code here:
		//		cbDanhSachKhachHang.setLocation(txTenKH.getLocation());
		//		cbDanhSachKhachHang.setSize(txTenKH.getSize());
		//		cbDanhSachKhachHang.setVisible(cbModelDanhSachKhachHang.getSize() > 1);
       }//GEN-LAST:event_jPanel2ComponentResized

       private void jPanel2AncestorResized(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_jPanel2AncestorResized
		// TODO add your handling code here:
       }//GEN-LAST:event_jPanel2AncestorResized
	private void kiemTraSoDT() {
		try {
			Integer.parseInt(txSDT.getText().trim());
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Nhập sai số giá. Nhập lại:", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
			txSDT.selectAll();
			txSDT.requestFocus();

			return;
		}
	}
       private void txSDTKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txSDTKeyReleased
		// TODO add your handling code here:
		kiemTraSoDT();
       }//GEN-LAST:event_txSDTKeyReleased
       // Variables declaration - do not modify//GEN-BEGIN:variables
       private javax.swing.JButton btInHoaDon;
       private javax.swing.JButton btKhachMoi;
       private javax.swing.JButton btLapHoaDon;
       private javax.swing.JButton btThem;
       private javax.swing.JButton btXoa;
       private javax.swing.JComboBox cbLoaiSP;
       private javax.swing.JComboBox cbSanPham;
       private javax.swing.JComboBox cbTT;
       private javax.swing.JButton jButton1;
       private javax.swing.JButton jButton2;
       private javax.swing.JCheckBox jCheckBox1;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel10;
       private javax.swing.JLabel jLabel12;
       private javax.swing.JLabel jLabel13;
       private javax.swing.JLabel jLabel14;
       private javax.swing.JLabel jLabel15;
       private javax.swing.JLabel jLabel16;
       private javax.swing.JLabel jLabel17;
       private javax.swing.JLabel jLabel18;
       private javax.swing.JLabel jLabel19;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel3;
       private javax.swing.JLabel jLabel4;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JLabel jLabel6;
       private javax.swing.JLabel jLabel7;
       private javax.swing.JLabel jLabel8;
       private javax.swing.JLabel jLabel9;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPanel jPanel3;
       private javax.swing.JPanel jPanel4;
       private javax.swing.JPanel jPanel5;
       private javax.swing.JPanel jPanel6;
       private javax.swing.JPanel jPanel7;
       private javax.swing.JScrollPane jScrollPane1;
       private javax.swing.JLabel lbCoTheBan;
       private javax.swing.JLabel lbGia;
       private javax.swing.JTable tbChiTiet;
       private javax.swing.JTextField txGiam;
       private javax.swing.JTextField txMaHoaDon;
       private javax.swing.JTextField txMaKH;
       private javax.swing.JTextField txMaNV;
       private javax.swing.JTextField txNgayLap;
       private javax.swing.JTextField txSDT;
       private javax.swing.JTextField txSoLuong;
       private javax.swing.JTextField txTenKH;
       private javax.swing.JTextField txTenNV;
       private javax.swing.JTextField txTriGia;
       // End of variables declaration//GEN-END:variables
	private javax.swing.JComboBox cbDanhSachKhachHang;
}
